<?php
/**
 * Created by PhpStorm.
 * User: candy
 * Date: 2017/3/12
 * Time: 下午11:28
 */
namespace app\index\controller;
use think\Controller;
use app\index\model\Program;

class Programs extends Controller
{

    public function addProgram($password, $program_name, $program_url, $program_type, $program_logo = '') {
        if ($password != 'www.90candy.com/115.159.157.222') {
            return myJson(-1, '节目添加失败 - 权限不足', '');
        }
        //不能为空
        if (empty($program_name) || empty($program_url)) {
            return myJson(-1, '节目名称或者节目源不能为空', '');
        }
        $program = new Program();
        if ($program->where('program_url', $program_url)->count() > 0) {
            return myJson(-1, '节目添加失败 - 节目已存在' , '');
        }

        try {
            $program->program_name  =   $program_name;
            $program->program_url   =   $program_url;
            $program->program_logo  =   $program_logo;
            $program->program_type  =   $program_type;
            $program->program_add_time  =   date('Y-m-d H:m:s');
            $program->save();
            return myJson(1, '节目添加成功', '');
        } catch (\Exception $e) {
            return myJson(-1, '节目添加失败 - ' . $e->getMessage(), '');
        }

    }

    //删除节目源
    public function deleteProgram($password, $program_name) {
        //暂时不管
    }


    //按条件查询所有节目 ---$type(查询条件)*---
    //  1->央视频道
    //  2->电影频道
    //  3->卫视频道
    public function selectByTypeId($type, $page_index) {
        // 逻辑处理
        $page_num = 10; //每次返回10条数据
        $start_index = ($page_index - 1) * $page_num;

        $program = new Program();
        $results = $program
            ->where('program_type', $type)
            ->limit($start_index, $page_num)
            ->select();
        if (!$results) {
            return myJson(-1, '没有更多数据', '');
        }
        return myJson(1, '数据查询成功', $results);
    }
 }